import { Author } from 'author-model.js';
import { Home } from '../home/home-model.js';
var author = new Author();
var home = new Home();
Page({
  data: {
    loadingHidden:false,
    inputShowed: false,
    inputVal: "",
    page:1,
    searchLoading: false,
    searchLoadingComplete: false,
    'article_cid':
      {
        "id1": 11,  /*需要读取文章分类的id,id从小到大设置 ,11是坭兴传奇*/
        "id2": 12   /* 12是大师风采 */
      }
  },

  onLoad: function (options) {
    this._loadData();
  },
  _loadData:function(){
    var that = this;
    //获得文章列表信息
    author.getArticleData(12,this.data.page,10,this.data.inputVal, (data) => {
      that.setData({
        articleArr2: data.items,
        loadingHidden: true,
        pageCount:data._meta.pageCount
      });
    });
    
    //获得分类标题
    home.getCateData(this.data.article_cid, (data) => {
      wx.setNavigationBarTitle({
        title: data[1].title
      })
      that.setData({
        CateNames: data,
      });
    });

  },
  /*搜索*/
  showInput: function () {
    this.setData({
      inputShowed: true,
      hidden: true
    });
  },
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false,
      hidden: false
    });
  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value
    });
  },
  searchBtn: function () {
    var page = 1;
    this._loadData();
  },

  //触底滚动加载
  onReachBottom: function () {
    var page = parseInt(this.data.page) + 1;
    if (page <= this.data.pageCount) {
      this.setData({
        searchLoading: true,
      })
      author.getArticleData(12,page, 10,this.data.inputVal, (res) => {
        this.scrollLoadData(res, page);
      })
    }
  },
  //拼接数据
  scrollLoadData: function (res, page) {
    var data = res.items;
    var articleArr2 = this.data.articleArr2;
    articleArr2 = articleArr2.concat(data)
    this.setData({
      articleArr2: articleArr2,
      page: page,
      pageCount: res._meta.pageCount,
    })
    if (page == this.data.pageCount) {
      this.setData({
        searchLoading: false,
        searchLoadingComplete: true,
      })
    }
  },

  /*跳转到作者详情*/
  onAuthorsItemTap: function (event) {
    var id = home.getDataSet(event, 'id');
    wx.navigateTo({
      url: '../author/author?id=' + id
    })
  },

})